package org.sys.transporttrainsys.controller;

import org.sys.transporttrainsys.entity.OrganizationType;
import org.sys.transporttrainsys.service.OrganizationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/organization-types")
@CrossOrigin(origins = "http://localhost:5173")
public class OrganizationTypeController {

    @Autowired
    private OrganizationTypeService organizationTypeService;

    @GetMapping
    public List<OrganizationType> getAllOrganizationTypes() {
        return organizationTypeService.getAllOrganizationTypes();
    }

    @GetMapping("/search")
    public List<OrganizationType> searchOrganizationTypes(@RequestParam String name) {
        return organizationTypeService.searchOrganizationTypesByName(name);
    }

    @PostMapping
    public OrganizationType createOrganizationType(@RequestBody OrganizationType organizationType) {
        return organizationTypeService.createOrganizationType(organizationType);
    }

    @PutMapping("/{id}")
    public ResponseEntity<OrganizationType> updateOrganizationType(@PathVariable Long id, @RequestBody OrganizationType organizationType) {
        OrganizationType updatedType = organizationTypeService.updateOrganizationType(id, organizationType);
        return ResponseEntity.ok(updatedType);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteOrganizationType(@PathVariable Long id) {
        organizationTypeService.deleteOrganizationType(id);
        return ResponseEntity.noContent().build();
    }
}